<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/2
 * Time: 13:10
 */

namespace App\Game;


use Carbon\Carbon;
use App\Game\Traits\ScTrait;

class sc extends Game
{
    use ScTrait;

    private $platName = 'sc';


    const API_PLAYER_LOGIN = '/login/thirdparty/';
    const API_PLAYER_BALANCE = '/api/v1/general/wallet/moreUserBalance';
    const API_TRANSFER_IN = '/api/v1/general/wallet/recharge';
    const API_TRANSFER_OUT = '/api/v1/general/wallet/withdraw';
    const API_BET_RECORD = '/api/v1/general/game/projectsHistory';


    public function __construct()
    {
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'plat_type' => 'lottery',
        ];
    }

    /**
     * gameapi
     * 2018/9/11 10:13
     * Administrator
     * getLength
     *
     * @return mixed
     */
    public function getLength()
    {
        return $this->length;
    }

    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * getPre
     * 获取前缀
     * @return mixed
     */
    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }


    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * curlCreateMember
     * 创建账号
     * @param $accountUserName
     * @param $password
     * @param $request
     * @return array|mixed
     */
    public function curlCreateMember($accountUserName, $password, $request)
    {
        return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
    }

    /**
     * gameapi
     * 2018/9/11 0:08
     * Administrator
     * curlGetBalance
     * 获取余额
     * @param $accountUserName
     * @return array|mixed
     */
    public function curlGetBalance($accountUserName)
    {

        $params = [
            'userArray' => [$accountUserName],
            'operator'=>$this->operator
        ];
        $result = $this->scRequest($params, self::API_PLAYER_BALANCE);

        if (array_key_exists('result', $result)) {
            if ($result['result'] == 0) {
                return ['success' => true, 'data' => ['balance' => $result['data']['userList'][0][$accountUserName],], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '创建用户失败，返回码：' . $result['isSuccess']];
            }
        } else {
            return ['success' => false, 'data' => [], 'message' => '请求平台失败，状态未知'];
        }

    }

    /**
     * gameapi
     * 2018/12/3 16:58
     * Administrator
     * curlTransferIn
     * 账号转入
     * @param $accountUserName
     * @param $price
     * @param $transferid
     * @return array
     * @throws \Exception
     */
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $accessUserToken = $this->userToken($accountUserName, null, request('username'));
        $accessMoneyToken = $this->rechargeToken($accountUserName, request('username'), $price, 'in');
        $operator = $this->operator;
        $userId = $accountUserName;
        $money = $price;

        $result = $this->scRequest(compact('accessUserToken', 'accessMoneyToken', 'operator', 'userId', 'money'), self::API_TRANSFER_IN);

        if (array_key_exists('result', $result)) {
            if ($result['result'] === "0") {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败，失败错：' . $result['message']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }


    }

    /**
     * gameapi
     * 2018/9/11 0:09
     * Administrator
     * curlTransferTo
     * 额度转出
     * @param $accountUserName
     * @param $price
     * @param $transferid
     * @return array|mixed
     */
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $accessUserToken = $this->userToken($accountUserName, null, request('username'));
        $accessMoneyToken = $this->rechargeToken($accountUserName, request('username'), $price, 'out');
        $operator = $this->operator;
        $userId = $accountUserName;
        $money = $price;

        $result = $this->scRequest(compact('accessUserToken', 'accessMoneyToken', 'operator', 'userId', 'money'), self::API_TRANSFER_OUT);
        if (array_key_exists('result', $result)) {
            if ($result['result'] === "0") {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败，失败错：' . $result['message']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    /**
     * gameapi
     * 2018/9/11 0:10
     * Administrator
     * curlCheckTransfer
     * 检查订单状态
     * @param $request
     * @return array|mixed
     */
    public function curlCheckTransfer($request)
    {
        return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '此平台没有检查订单状态此功能'];
    }

    public function curlKick($request)
    {
        return ['success' => false, 'data' => [], 'message' => '此平台没有踢线此功能'];
    }

    /**
     * gameapi
     * 2018/11/29 14:26
     * Administrator
     * curlJoinGame
     * 游戏登录
     * @param $request
     * @return array
     * @throws \Exception
     */
    public function curlJoinGame($request)
    {
        $token = $this->userToken($request->accountUserName, $request->password, $request->username);
        $url = $this->gameUrl . self::API_PLAYER_LOGIN . $this->roomMasterId . '-' . $this->roomMasterHash . '?';
        $params = [
            'token' => $token,
            'op' => $this->operator,
            'lang' => 'cn',
        ];

        if (!(boolean)$request->is_lobby) {
            $params = array_merge($params, ['lotteryId' => $request->gamecode]);
        }

        $end = array_search(end($params), $params);
        foreach ($params as $key => $param) {
            if ($key == $end) {
                $url .= $key . '=' . $param;
            } else {
                $url .= $key . '=' . $param . '&';
            }
        }

        if ($token) {
            return ['success' => true, 'data' => ['url' => $url], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '游戏登录失败，登录token未获取到'];
        }

    }

    /**
     * gameapi
     * 2018/9/11 0:10
     * Administrator
     * curlJoinMobileGame
     * 手机端游戏登录
     * @param $request
     * @return array
     */
    public function curlJoinMobileGame($request)
    {
        return $this->curlJoinGame($request);
    }

    /**
     * gameapi
     * 2018/11/22 19:22
     * Administrator
     * curlGetBetRecord
     * 获取记录
     * @param Carbon $startTime
     * @param Carbon $endTime
     * @return array
     */
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $data = [];
        $start_time = $startTime->toDateTimeString();
        $end_time = $endTime->toDateTimeString();
//        $start_time = '2019-07-13 00:12:15';
//        $end_time = '2019-07-13 00:15:15';
        $pageLimit = 500;
        $page = $totalPage = 1;

        do {
            $params = [
                'startTime' => $start_time,
                'endTime' => $end_time,
                'page' => $page,
                'pageLimit' => $pageLimit,
            ];
            $result = $this->scRequest($params, self::API_BET_RECORD);
            if (is_array($result)) {
                if (array_key_exists('result', $result)) {
                    if ($result['result'] == 0) {
                        $betRecords = $result['data']['list'];
                        $totalPage = $result['pagination']['totalPage'];
                        if (count($betRecords) >= 1) {
                            $data = array_merge($data, $betRecords);
                        }
                    }
                }
            }
            $page++;
        } while ($page <= $totalPage);
        return $data;
    }
}